package com.hrt.datasender.dataschedule;

import java.util.HashMap;
import java.util.List;

import com.hrt.datasender.process.strategy.hj212.MessagePraser;
import org.quartz.JobExecutionContext;

import com.btcode.common.MyLog;
import com.btcode.db.result.Row;
import com.btcode.log.ILog;
import com.btcode.schedule.SingleInstanceJob;
import com.hrt.datasender.datadao.model.BcSendPlatformDataStrategyModel;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BcSendPlatformDataStrategyService;
import com.hrt.datasender.datadao.service.BcSendPlatformService;
import com.hrt.datasender.datastorage.strategy.IStorageStrategy;
import com.hrt.datasender.datastorage.strategy.StrategyLoader;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.model.PacketModel;
import com.hrt.datasender.process.strategy.IProcessStrategy;
import com.hrt.datasender.process.strategy.ProcessStrategyLoader;

/**
 * 定时从存储获取数据并打包发送
 */
public class DataStorageScheduler extends SingleInstanceJob {

    private ILog log;

    private List<BcSendPlatformModel> platformList = null;

    private HashMap<String, List<BcSendPlatformDataStrategyModel>> platFromMap;

    @Override
    public void initJob(JobExecutionContext arg0) {

        log = MyLog.getInstance().getLogger(getClass());
    }

    /**
     * 1、循环所有发送平台 <br>
     * 2、获取当前平台的数据策略和解析策略集合 <br>
     * 3、循环获取当前平台的数据策略和解析策略集合<strong>实例</strong> <br>
     * 4、调用数据策略的<strong>getSendData</strong>接口,获取要发送的数据 <br>
     * 5、调用解析策略的<strong>packet</strong>接口，打包数据 <br>
     * 6、封装成<strong>PacketModel</strong>对象，发布消息
     */
    @Override
    public void myExecute(JobExecutionContext arg0) {
        log.info("进入myExecute");

        if (platformList == null) {
            platformList = BcSendPlatformService.getInstance().getModelList();
        }
        log.info("进入platformList");

        if (platFromMap == null) {
            platFromMap = new HashMap<>();
        }

        for (BcSendPlatformModel platform : platformList) {

            String ip = platform.getIp();
            String port = platform.getPort();
            String mn = platform.getMn();
            String platformId = platform.getPlatformId();

            if (mn == null || mn.equals("")) {
                log.error("平台：" + platform.getName() + "没有mn号");
                continue;
            }

            List<BcSendPlatformDataStrategyModel> strategyModels = null;

            if (!platFromMap.containsKey(platform.getPlatformId())) {
                strategyModels = BcSendPlatformDataStrategyService.getInstance().getList(platformId);
                platFromMap.put(platformId, strategyModels);
            }

            strategyModels = platFromMap.get(platformId);

            for (BcSendPlatformDataStrategyModel strategy : strategyModels) {

                String storageStrategyId = strategy.getStorageStrategyId();
                String processStrategyId = strategy.getProcessStrategyId();

                /**
                 * 允许数据策略为空
                 */
                if (storageStrategyId == null) {
                    continue;
                }

                if (processStrategyId == null) {
                    log.error("平台：" + platform.getName() + "的数据策略：" + storageStrategyId + ",没有对应的解析策略，检查发送平台配置");
                    continue;
                }

                try {

                    strategy.getStorageStrategyId();
                    strategy.getProcessStrategyId();

                    IStorageStrategy storageStrategy = StrategyLoader.getInstance().getStrategyById(storageStrategyId);
                    IProcessStrategy processStrategy = ProcessStrategyLoader.getInstance().getStrategyById(platformId,
                        processStrategyId);

                    if (storageStrategy == null) {
                        log.error("平台：" + platform.getName() + "的数据策略：" + storageStrategyId + "为空，检查发送平台配置");
                        continue;
                    }

                    if (processStrategy == null) {
                        log.error("平台：" + platform.getName() + "的数据策略：" + storageStrategyId + ",对应的解析策略:"
                            + processStrategyId + "为空，检查发送平台配置");
                        continue;
                    }

                    List<Row> sendData = storageStrategy.getSendData(platform);

                    if (sendData == null || sendData.size() <= 0) {
                        continue;
                    }

                    log.info(platform.getName() + "准备打包" + storageStrategy.getShowName() + ":" + storageStrategyId
                        + "数据，size:" + sendData.size());

                    Object msg = processStrategy.packet(sendData, platform);
                    log.info(storageStrategy.getShowName() + ":" + storageStrategyId + "打包完成");

                    PacketModel packetModel = new PacketModel();
                    packetModel.setIp(ip);
                    packetModel.setPort(port);
                    packetModel.setMn(mn);
                    packetModel.setMsg(msg);
                    packetModel.setDataType(storageStrategy.getDataType());
                    String qn = MessagePraser.getQN((String) msg);
                    packetModel.setQn(qn);
                    MqKit.productMsgToBizDataMQ(packetModel);

                }
                catch (Exception e) {
                    log.error("调度：" + storageStrategyId + "出错", e);
                }
            }

        }
    }

}
